using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PACKETPARSERMAX20.CORE;

using WeifenLuo.WinFormsUI.Docking;


namespace PACKETPARSERMAX20.WinUI
{
    public partial class frmFilterCustom : DockContent
    {
        public frmFilterCustom()
        {
            InitializeComponent();
            mSortManager = new ListViewSortManager(lstView, new Type[] { typeof(PACKETPARSERMAX20.ListViewInt32Sort), typeof(PACKETPARSERMAX20.ListViewTextSort), typeof(PACKETPARSERMAX20.ListViewTextSort) });
            this.Width = 400;
        }

        private ListViewSortManager mSortManager = null;
        private bool mUpdating = false;

        public void Init()
        {

            mUpdating = true;
            lstView.Items.Clear();
            for (int i = 0; i <= (int)Opcodes.NUM_MSG_TYPES; i++)
            {
                ListViewItem lvItem = lstView.Items.Add(i.ToString());
                lvItem.Checked = StaticObjects.opcodeFilter.Contains((Opcodes)i);
                lvItem.Tag = i;
                lvItem.SubItems.Add(((Opcodes)i).ToString());
                lvItem.SubItems.Add(lvItem.Checked.ToString());
            }

            lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
            lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            mUpdating = false;
        }

        private void lstView_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (!mUpdating) { UpdateItem(e.Item); ;}
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            mUpdating = true;
            foreach (ListViewItem tItem in lstView.Items)
            {
                tItem.Checked = true;
                UpdateItem(tItem);
            }
            mUpdating = false;
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            mUpdating = true;
            foreach (ListViewItem tItem in lstView.Items)
            {
                tItem.Checked = false;
                UpdateItem(tItem);
            }
            mUpdating = false;
        }

        private void btnSelectOnlySelected_Click(object sender, EventArgs e)
        {
            mUpdating = true;
            foreach (ListViewItem tItem in lstView.Items)
            {
                if (!lstView.SelectedItems.Contains(tItem))
                {
                    tItem.Checked = false;
                }
                else
                {
                    tItem.Checked = true;
                }
                UpdateItem(tItem);
            }
            mUpdating = false;
        }

        private void btnDefaults_Click(object sender, EventArgs e)
        {
            StaticObjects.opcodeFilter.LoadDefaults();
            Init();

        }

        private void btnSelectAllButSelected_Click(object sender, EventArgs e)
        {
            mUpdating = true;
            foreach (ListViewItem tItem in lstView.Items)
            {
                if (lstView.SelectedItems.Contains(tItem))
                {
                    tItem.Checked = false;
                }
                else
                {
                    tItem.Checked = true;
                }

                UpdateItem(tItem);
            }
            mUpdating = false;
        }

        private void UpdateItem(ListViewItem pItem)
        {
 
            int i = (int)pItem.Tag;
            if (pItem.Checked && !StaticObjects.opcodeFilter.Contains((Opcodes)i))
            {
                StaticObjects.opcodeFilter.Add((Opcodes)i);
            }
            else if (!pItem.Checked && StaticObjects.opcodeFilter.Contains((Opcodes)i))
            {
                StaticObjects.opcodeFilter.Remove((Opcodes)i);
            }
            pItem.SubItems[2].Text = pItem.Checked.ToString();
        }
    }
}